#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#define PATH "/dev/test_char_drv_01"
char read_buf[512] = {0};
char write_buf[256*10] = {0};


/*
// 获取当前进程的PID
	pid_t pid = getpid();
	printf("pid = %d\n", pid);
	
	// 获取当前进程的PPID
	pid_t ppid = getppid();
	printf("ppid = %d\n", ppid);
*/

#define TEST_CHAR_DRV_MAGIC  'k'
#define TEST_CHAR_DRV_CMD_INIT_PIPE    _IO(TEST_CHAR_DRV_MAGIC, 0x1a)
#define TEST_CHAR_DRV_CMD_INIT_PACKET    _IO(TEST_CHAR_DRV_MAGIC, 0x1b)


int main()
{
    int fd = 0;
	int len = 0;
	int i = 0;
	int pid = 0;
	int ret = 0;
	

	fd = open(PATH, O_RDWR);
	if (fd < 0)
	{
		printf("open file %s error!\n", PATH);
		return -1;
	}
	
	ret = ioctl(fd, TEST_CHAR_DRV_CMD_INIT_PACKET, NULL);
	if (ret < 0)
	{
		printf("ioctrl file %s error!\n", PATH);
		return -1;			
	}

	{
		sleep(1);
	
		while (1)
		{
			memset(read_buf, 0, sizeof(read_buf));
			len = read(fd, read_buf, sizeof(read_buf));

			printf("child read len=%d\n", len);

			for(i=0; i<len; i++)
				printf("0x%x ", read_buf[i] & 0xFF);

			printf("\n");

			fflush(stdout);
		}		
		close(fd);
	}

	
    return 0;
}
